package com.nchu.student_score.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nchu.student_score.dataObject.ClazzResult;
import com.nchu.student_score.mapper.ClazzMapper;
import com.nchu.student_score.mapper.StudentMapper;
import com.nchu.student_score.mapper.TeachClazzMapper;
import com.nchu.student_score.model.Clazz;
import com.nchu.student_score.model.Student;
import com.nchu.student_score.model.TeachClazz;
import com.nchu.student_score.service.ClazzService;
import com.nchu.student_score.util.Define;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    ClazzMapper clazzMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    TeachClazzMapper teachClazzMapper;

    @Override
    public PageInfo<ClazzResult> getClazzList(ClazzResult clazzResult, Integer currPage) {
        if(currPage == null) currPage = 1;
        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        return new PageInfo<>(clazzMapper.findClazzList(clazzResult));
    }

    @Override
    public ResultVo<Clazz> updateClazz(Clazz input, Integer oldClazzId) {
        ResultVo<Clazz> resultVo = new ResultVo<>();

        if(oldClazzId == -1){//新增班级
            Clazz clazz = clazzMapper.selectByPrimaryKey(input.getClazzId());
            if(clazz != null){
                resultVo.setStatus(1);
                resultVo.setMessage("目标班级编号已存在!");
                return resultVo;
            }

            clazzMapper.insert(input);
            resultVo.setStatus(0);
            resultVo.setMessage("新增班级 " + input.getClazzId() + " 成功!");
        }
        else{//修改班级
            if(! Objects.equals(input.getClazzId(), oldClazzId)){//需要修改班级编号
                Clazz clazz = clazzMapper.selectByPrimaryKey(input.getClazzId());
                if(clazz != null){
                    resultVo.setStatus(1);
                    resultVo.setMessage("目标班级编号已存在!");
                    return resultVo;
                }
                //查询原班级id是否已被使用
                List<Student> studentList = studentMapper.selectByClazzId(oldClazzId);
                List<TeachClazz> teachClazzList = teachClazzMapper.selectByClazzId(oldClazzId);
                if(studentList.size() > 0 || teachClazzList.size() > 0){//已被使用
                    resultVo.setStatus(3);
                    resultVo.setMessage("原班级当前正在使用中,不能修改!");
                    return resultVo;
                }
                else{//未使用
                    clazzMapper.deleteByPrimaryKey(oldClazzId);
                    clazzMapper.insert(input);
                }
            }
            else{//无需修改编号
                clazzMapper.updateByPrimaryKey(input);
            }

            resultVo.setStatus(0);
            resultVo.setMessage("修改班级成功!");
        }

        return resultVo;
    }

    @Override
    public ResultVo<ClazzResult> getClazz(Integer professionId) {
        ResultVo<ClazzResult> resultVo = new ResultVo<>();
        ClazzResult clazzResult = new ClazzResult();
        clazzResult.setProfessionId(professionId);
        resultVo.setList(clazzMapper.findClazzList(clazzResult));
        return resultVo;
    }

    @Override
    public ResultVo<ClazzResult> findClazz(Integer clazzId) {
        ResultVo<ClazzResult> resultVo = new ResultVo<>();
        if(clazzId == null || clazzId < 10){
            resultVo.setStatus(1);
            resultVo.setMessage("您输入的待搜索信息太少了!");
            return resultVo;
        }

        ClazzResult clazzResult = new ClazzResult();
        clazzResult.setClazzId(clazzId);
        resultVo.setList(clazzMapper.findClazzList(clazzResult));
        resultVo.setStatus(0);
        return resultVo;
    }
}
